// components/msg-item/item.js
import computedBehavior from '../../lib/miniprogram-computed/index'
import { previewImage } from '../../utils/previewImage.js'
import {baseUrl, dianZan} from '../../api/index'
import {authUserVerify} from '../../utils/authUserVerify'

Component({
  behaviors: [computedBehavior],
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object,
      value: {}
    },
    module: {
      type: String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    baseUrl: baseUrl
  },
  computed: {
    compItem () {
      return []
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindShowShare (e) {
      wx.showShareMenu({
        withShareTicket: true,
        success (res) {
          console.log(res, 'success')
        }
      })
    },
    bindPreviewImage (e) {
      let _dataset = e.currentTarget.dataset
      console.log(_dataset)
      previewImage(baseUrl + _dataset.src, _dataset.urls.map((item) => baseUrl + item))
    },
    // 详情
    bindNavToDetail(e) {
      let {id} = e.currentTarget.dataset
      let _url = '/pages/article/index?id=' + id
      console.log('bindNavToDetail msg-item', id)
      authUserVerify(this.properties.module, () => {
        wx.navigateTo({
          url: _url,
        })
      })
    },
    bindDianZan (e) {
      let _this = this
      let _dataset = e.currentTarget.dataset
      dianZan({aid: _dataset.id}).then(res => {
        let _isPick = res.data.status == 1 ? 1 : 0
        _this.triggerEvent('itemDianZan', {id: _dataset.id, is_pick: _isPick})
      }).catch(err => {
        console.log(err)
        wx.showToast({
          icon: 'none',
          title: err.data.info
        })
      })
    }
  }
})
